*** TABLE E3

clear 

use "$P_Data_Processed/monthly_duo_trio_mkt_data_e5_merged.dta"
 
sort mktid year_month StID

* for 2 station mkts 
by mktid year_month: gener one_rival_adopts = 0 if n_act_stations==2 
by mktid year_month: replace one_rival_adopts = 1 if _n==1 & treat[1]==0 & treat[2]==1 & n_act_stations==2
by mktid year_month: replace one_rival_adopts = 1 if _n==2 & treat[2]==0 & treat[1]==0 & n_act_stations==2
* for 3 station mkts 
by mktid year_month: replace one_rival_adopts = 0 if n_act_stations==3
by mktid year_month: replace one_rival_adopts = 1 if _n==1 & treat[1]==0 & treat[2]==1 & treat[3]==0 & n_act_stations==3
by mktid year_month: replace one_rival_adopts = 1 if _n==1 & treat[1]==0 & treat[2]==0 & treat[3]==1 & n_act_stations==3
by mktid year_month: replace one_rival_adopts = 1 if _n==2 & treat[2]==0 & treat[1]==1 & treat[3]==0 & n_act_stations==3
by mktid year_month: replace one_rival_adopts = 1 if _n==2 & treat[2]==0 & treat[1]==0 & treat[3]==1 & n_act_stations==3
by mktid year_month: replace one_rival_adopts = 1 if _n==3 & treat[3]==0 & treat[1]==1 & treat[2]==0 & n_act_stations==3
by mktid year_month: replace one_rival_adopts = 1 if _n==3 & treat[3]==0 & treat[1]==0 & treat[2]==1 & n_act_stations==3

* two rivals adopt IV
by mktid year_month: gener two_rivals_adopt = 0 
by mktid year_month: replace two_rivals_adopt = 1 if _n==1 & treat[1]==0 & treat[2]==1 & treat[3]==1 & n_act_stations==3
by mktid year_month: replace two_rivals_adopt = 1 if _n==2 & treat[1]==1 & treat[2]==0 & treat[3]==1 & n_act_stations==3
by mktid year_month: replace two_rivals_adopt = 1 if _n==3 & treat[1]==1 & treat[2]==1 & treat[3]==0 & n_act_stations==3

* IVs* 
* for 2 station mkts 
by mktid year_month: gener one_rival_adopts_IV = 0 if n_act_stations==2 
by mktid year_month: replace one_rival_adopts_IV = share_others_treated[2] if _n==1 & treat[1]==0 & treat[2]==1 & n_act_stations==2
by mktid year_month: replace one_rival_adopts_IV = share_others_treated[1] if _n==2 & treat[2]==0 & treat[1]==0 & n_act_stations==2
* for 3 station mkts 
by mktid year_month: replace one_rival_adopts_IV = 0 if n_act_stations==3
by mktid year_month: replace one_rival_adopts_IV = share_others_treated[2] if _n==1 & treat[1]==0 & treat[2]==1 & treat[3]==0 & n_act_stations==3
by mktid year_month: replace one_rival_adopts_IV =  share_others_treated[3] if _n==1 & treat[1]==0 & treat[2]==0 & treat[3]==1 & n_act_stations==3
by mktid year_month: replace one_rival_adopts_IV =  share_others_treated[1] if _n==2 & treat[2]==0 & treat[1]==1 & treat[3]==0 & n_act_stations==3
by mktid year_month: replace one_rival_adopts_IV =  share_others_treated[3] if _n==2 & treat[2]==0 & treat[1]==0 & treat[3]==1 & n_act_stations==3
by mktid year_month: replace one_rival_adopts_IV =  share_others_treated[1] if _n==3 & treat[3]==0 & treat[1]==1 & treat[2]==0 & n_act_stations==3
by mktid year_month: replace one_rival_adopts_IV =  share_others_treated[2] if _n==3 & treat[3]==0 & treat[1]==0 & treat[2]==1 & n_act_stations==3
	

label variable one_rival_adopts "One Rival Adopted"
label variable two_rivals_adopt "Two Rivals Adopted (in Triopoly Market)"

by mktid year_month: gener two_rivals_adopt_IV = 0 
by mktid year_month: replace two_rivals_adopt_IV = share_others_treated[2]*share_others_treated[3] if _n==1 & n_act_stations==3  & treat[1]==0 & treat[2]==1 & treat[3]==1
by mktid year_month: replace two_rivals_adopt_IV = share_others_treated[1]*share_others_treated[3] if _n==2 & n_act_stations==3 & treat[1]==1 & treat[2]==0 & treat[3]==1
 by mktid year_month: replace two_rivals_adopt_IV = share_others_treated[1]*share_others_treated[2] if _n==3 & n_act_stations==3 & treat[1]==1 & treat[2]==1 & treat[3]==0
 
ivreghdfe mean_wh_margin l_gdp pop_density med_age employed_share l_pop n_brand mean_temp sd_temp mean_precip sd_precip (one_rival_adopts two_rivals_adopt = one_rival_adopts_IV two_rivals_adopt_IV) if T2==0 & treat==0, absorb(StID year_month) cluster(mktid)
 	 outreg2 using "$P_Tables/Table_E3",  tex(frag) dec(3) label nor2 replace  keep(one_rival_adopts two_rivals_adopt)
 
 
 ivreghdfe mean_price l_gdp pop_density med_age employed_share l_pop n_brand mean_temp sd_temp mean_precip sd_precip (one_rival_adopts two_rivals_adopt = one_rival_adopts_IV two_rivals_adopt_IV) if T2==0 & treat==0, absorb(StID year_month) cluster(mktid)
 	 outreg2 using "$P_Tables/Table_E3",  tex(frag) dec(3) label nor2 append  keep(one_rival_adopts two_rivals_adopt)
